package com.ytjj.qmyx.admin.controller;

import com.ytjj.common.api.CommonPage;
import com.ytjj.common.api.CommonResult;
import com.ytjj.qmyx.admin.eunm.LabelTypeEnums;
import com.ytjj.qmyx.admin.model.request.LabelRequest;
import com.ytjj.qmyx.admin.model.response.LabelResponse;
import com.ytjj.qmyx.admin.service.LabelService;
import com.ytjj.qmyx.users.model.Label;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/label")
/**
 * 标签管理
 */
public class LabelController {
    @Autowired
    private LabelService labelService;

    /**
     * 列表
     *
     * @return
     */
    @PostMapping("/list")
    public CommonResult list(@RequestBody LabelRequest labelRequest) {
        CommonPage<LabelResponse> list = labelService.list(labelRequest);
        return CommonResult.success(list);
    }

    /**
     * 新增标签
     */
    @PostMapping("/insert")
    public CommonResult insertLabel(@RequestBody LabelRequest labelRequest) {
        int count = labelService.insertLabel(labelRequest);
        if (count > 0){
            return CommonResult.success(count);
        }else {
            return CommonResult.failed();
        }
    }

    /**
     * 修改标签
     */
    @PostMapping("/update")
    public CommonResult updateLabel(@RequestBody LabelRequest labelRequest) {
        int count = labelService.updateLabel(labelRequest);
        if (count > 0){
            return CommonResult.success(count);
        }else {
            return CommonResult.failed();
        }
    }

    /**
     * 获取类别标签
     * @return
     */
    @GetMapping("/labelTypes")
    public CommonResult labelTypes() {
        LabelTypeEnums[] values = LabelTypeEnums.values();
        List<Map<String, String>> result = new ArrayList();
        for (LabelTypeEnums value : values) {
            Map<String, String> map = new HashMap<>();
            map.put(value.getCode(), value.getType());
            result.add(map);
        }
        return CommonResult.success(result);
    }

    /**
     * 删除标签
     * @param labelRequest
     * @return
     */
    @PostMapping("/deleteLabel")
    public CommonResult deleteLabel(@RequestBody LabelRequest labelRequest){
        int count = labelService.deleteLabel(labelRequest.getId());
        if (count > 0){
            return CommonResult.success(count);
        }else {
            return CommonResult.failed();
        }
    }
}
